package com.rayenterprise.kms.server;

import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;

import javax.servlet.ServletException;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

import net.sf.gilead.core.PersistentBeanManager;
import net.sf.gilead.core.hibernate.HibernateUtil;
import net.sf.gilead.core.store.stateless.StatelessProxyStore;
import net.sf.gilead.gwt.PersistentRemoteService;

import com.rayenterprise.kms.client.remote.HolderRemote;

import com.rayenterprise.kms.domain.KHolder;

public class HolderRemoteService extends PersistentRemoteService implements
		HolderRemote {

	private static final long serialVersionUID = -4633056900613169523L;

	@Override
	public void init() throws ServletException {
		super.init();

		// Bean Manager initialization
		//
		HibernateUtil persistenceUtil = new HibernateUtil();
		persistenceUtil.setSessionFactory(HibernateContext.getSessionFactory());

		PersistentBeanManager beanManager = new PersistentBeanManager();
		beanManager.setPersistenceUtil(persistenceUtil);
		beanManager.setProxyStore(new StatelessProxyStore());
		setBeanManager(beanManager);

	}

	@Override
	public List<KHolder> getAll() {
		Session session = null;
		StringBuffer sb = new StringBuffer("");
		sb.append("from KHolder holder left join fetch holder.notions notions order by holder.date");
		try {
			session = HibernateContext.getSessionFactory().openSession();
			Query q = session.createQuery(sb.toString());
			Set<KHolder> uniqueHolders = new LinkedHashSet<KHolder>(q.list());
			List<KHolder> list = new ArrayList<KHolder>(uniqueHolders);
			return list;
		} catch (RuntimeException e) {

			throw e;
		} finally {
			if (session != null) {
				session.close();
			}
		}
	}
	/**
	 * Get the holder and subsidiary notion 
	 */
	@Override
	public KHolder getHolder(int holderId) {
		Session session = null;
		StringBuffer sb = new StringBuffer("");
		sb.append("from KHolder holder left fetch holder.notions notions where holderId =");
		sb.append(holderId);
		try {
			session = HibernateContext.getSessionFactory().openSession();
			Query q = session.createQuery(sb.toString());
			return (KHolder) q.uniqueResult();
		} catch (RuntimeException e) {

			throw e;
		} finally {
			if (session != null) {
				session.close();
			}
		}
	}

	@Override
	public KHolder save(KHolder holder) {
		Session session = null;
		Transaction transaction = null;
		
		try {
			session = HibernateContext.getSessionFactory().openSession();
			transaction = session.beginTransaction();
			session.saveOrUpdate(holder);
			transaction.commit();
			return holder;
		} catch (RuntimeException e) {
			transaction.rollback();
			throw e;
		} finally {
			if (session != null) {
				session.close();
				
			}
		}
	}

	@Override
	public void deleteHolder(KHolder holder) {
		// TODO Auto-generated method stub
		Session session = null;
		Transaction transaction = null;
		
		try {
			session = HibernateContext.getSessionFactory().openSession();
			transaction = session.beginTransaction();
			session.delete(holder);
			transaction.commit();
			
		} catch (RuntimeException e) {
			transaction.rollback();
			throw e;
		} finally {
			if (session != null) {
				session.close();
				
			}
		}
	}

}
